package ca.kooki.codesnippet;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import ca.kooki.codesnippet.core.PreferenceFacade;
import ca.kooki.codesnippet.core.User;
import ca.kooki.codesnippet.views.TagEntryView;

public class Activator extends AbstractUIPlugin {

  //```````````````````````
  // STATIC VARIABLES
  //```````````````````````
  
	public static final String PLUGIN_ID = "ca.kooki.codesnippet";
  public static final String TAG_VIEW_ID = "ca.kooki.codesnippet.views.TagEntryView";
	private static Activator plugin;

  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````
  
  private PreferenceFacade _preferences; 

  //```````````````````````
  // CONSTRUCTOR VARIABLES
  //```````````````````````

	public Activator() {
    init();
	}

  private void init()
  {
    plugin = this;
    _preferences = null;
  }

  //```````````````````````
  // INTERFACE
  //```````````````````````
  
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	public static Activator getDefault() {
		return plugin;
	}
  
  public static void loadDefaultAsRequired()
  {
    if (plugin == null)
    {
      plugin = new Activator();
    }
  }
  
  public static void resetDefault()
  {
    plugin.init();
  }
  
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
  
  protected void initializeDefaultPreferences(IPreferenceStore store) {
    store.setDefault(CodeSnippetPreferencePage.REPOSITORY_FILE_PREFERENCE,CodeSnippetPreferencePage.DEFAULT_REPOSITORY_FILE_PREFERENCE);
  }

  public TagEntryView showTagEntryView()
  {
    try 
    {
      return (TagEntryView)getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(TAG_VIEW_ID);
    } 
    catch (PartInitException e) 
    {
      return null;
    }
  }
  
  public PreferenceFacade getPreferences()
  {
    if (_preferences == null)
    {
      _preferences = new PreferenceFacade(this);
    }
    return _preferences;
  }

  public CodeSnippetRepository getRepository() {
    
    String filename = getPreferences().getString(CodeSnippetPreferencePage.REPOSITORY_FILE_PREFERENCE);
    
    CodeSnippetRepository repository = null;
    if (filename == "" || filename == "InMemory")
    {
      repository = InMemoryCodeSnippetRepository.getDefault();
    }
    else
    {
      repository = new FileSystemCodeSnippetRepository(filename);
    }
    
    String username = getPreferences().getString(CodeSnippetPreferencePage.USER_PREFERENCE);
    
    if (username != "")
    {
      User user = repository.getUserByUsername(username);
      
      if (!user.isOfficial())
      {
        repository.addUser(user);
      }
      
      repository.setUser(user);
    }
    else
    {
      repository.setUser((User)null);
    }
    
    return repository;
  }
  
  
}
